package com.educhains.module.message.service.impl;

import com.educhains.module.message.persist.mybatis.EmailMapper;
import com.educhains.module.message.service.EmailService;
import com.educhains.module.message.util.EmailUtils;
import com.educhains.module.message.vo.Email;
import com.educhains.web.util.PropertyUtils;
import com.educhains.web.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author YuRun
 * @since 2017年8月22日
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private EmailMapper emailMapper = null;

    private static String MODULE_FOR_REGISTER = "<div style='width: 80%%; height: 160px; border: 1px solid #004a86; margin-top: 10px; border-top: 45px solid #004a86;padding: 10px;margin-left: 10%%;' >您好<br>欢迎加入EDU Chains<br>请点击以下进行激活<br><a href='%s'>%s</a><br>该链接在一小时之内有效<br>如果您错误的收到了这封邮件，请忽略</div>";

    private static String MODULE_FOR_RESET_PASSWORD = "<div style='width: 80%%; height: 160px; border: 1px solid #004a86; margin-top: 10px; border-top: 45px solid #004a86;padding: 10px;margin-left: 10%%;' >您好<br>您正在请求EDU Chains密码重置服务<br>请点击以下链接确定重置密码<br><a href='%s'>%s</a><br>该链接在一小时之内有效<br>如果您错误的收到了这封邮件，请忽略</div>";

    public boolean sendActivateEmail(String url, String toEmail) throws Exception {
        String token = StringUtils.generateRandomString(16);
        String activateUrl = url + "/activateEmail?token=" + token + "&email=" + toEmail;
        String content = String.format(MODULE_FOR_REGISTER, activateUrl, activateUrl);
        Email dbEmail = emailMapper.selectByPrimaryKey(toEmail);
        Calendar calendar = Calendar.getInstance();
        Date createTime = calendar.getTime();
        calendar.add(Calendar.SECOND, 60 * 60);
        Date expireTime = calendar.getTime();
        //Email exists
        if (dbEmail != null) {
            //if (createTime.before(dbEmail.getExpireTime())) {
            //    return true;
            //}
            //Expired and resend
            dbEmail.setCode(token);
            dbEmail.setCreateTime(createTime);
            dbEmail.setExpireTime(expireTime);
            emailMapper.updateByPrimaryKey(dbEmail);
            EmailUtils.sendEmail(PropertyUtils.getProperty("email.address"), PropertyUtils.getProperty("email.password"), content, toEmail);
        }
        //New email
        else {
            Email email = new Email();
            email.setCode(token);
            email.setEmail(toEmail);
            email.setCreateTime(createTime);
            email.setExpireTime(expireTime);
            emailMapper.insert(email);
            EmailUtils.sendEmail(PropertyUtils.getProperty("email.address"), PropertyUtils.getProperty("email.password"), content, toEmail);
        }
        return true;
    }

    public boolean checkActivateEmail(String token, String email) {
        Email dbEmail = emailMapper.selectByPrimaryKey(email);
        Date now = new Date();
        if (dbEmail == null) {
            return false;
        }
        if (now.after(dbEmail.getExpireTime())) {
            return false;
        }
        if (token.compareTo(dbEmail.getCode()) == 0) {
            return true;
        }
        return true;
    }

    public boolean sendResetPasswordEmail(String url, String toEmail, String userPassword) throws Exception {
        //FIXME encrypt the info
        String token = StringUtils.generateRandomString(16);
        String resetUrl = url + "/resetPasswordEmail?email=" + toEmail + "&password=" + userPassword + "&token=" + token;
        String content = String.format(MODULE_FOR_RESET_PASSWORD, resetUrl, resetUrl);

        Email dbEmail = emailMapper.selectByPrimaryKey(toEmail);
        Calendar calendar = Calendar.getInstance();
        Date createTime = calendar.getTime();
        calendar.add(Calendar.SECOND, 60 * 60);
        Date expireTime = calendar.getTime();
        if (dbEmail == null) {
            return false;
        }
        //FIXME may reset email in one hour after register?
        //if (createTime.before(dbEmail.getExpireTime())) {
        //    return true;
        //}
        //Expired and resend
        dbEmail.setCode(token);
        dbEmail.setCreateTime(createTime);
        dbEmail.setExpireTime(expireTime);
        emailMapper.updateByPrimaryKey(dbEmail);
        return EmailUtils.sendEmail(PropertyUtils.getProperty("email.address"), PropertyUtils.getProperty("email.password"), content, toEmail);
    }

    public boolean checkResetPasswordEmail(String token, String email) {
        Email dbEmail = emailMapper.selectByPrimaryKey(email);
        Date now = new Date();
        if (dbEmail == null) {
            return false;
        }
        if (now.after(dbEmail.getExpireTime())) {
            return false;
        }
        if (token.compareTo(dbEmail.getCode()) == 0) {
            return true;
        }
        return true;
    }
}
